/*
 * NetworkPanel.java
 *
 * Created on February 13, 2008, 3:00 AM
 */

package gui.options;

import datamodel.WSFConfiguration;
import gui.WSFApplication;
import org.apache.log4j.Logger;

/**
 *
 * @author  chang
 */
public class NetworkPanel extends javax.swing.JPanel {
    private static Logger logger = Logger.getLogger(NetworkPanel.class);
    private boolean changed;
    
    /** Creates new form NetworkPanel */
    public NetworkPanel() {
        initComponents();
        
        postInit();
    }
    
    private void postInit(){
        
        changed = false;
        
        WSFConfiguration config = WSFApplication.getApplication().getWSFConfiguration();
        maxNOCPerHostSpinner.setValue(config.getMaxNumberOfConnectionsPerHost());
        maxNOCOverallSpinner.setValue(config.getMaxNumberOfConnectionsOverall());
    }
    
    public void saveChanges(){
        
        if(!changed)
            return;
        
        WSFConfiguration config = WSFApplication.getApplication().getWSFConfiguration();
        config.setMaxNumberOfConnectionsPerHost((Integer)maxNOCPerHostSpinner.getValue());
        config.setMaxNumberOfConnectionsOverall((Integer)maxNOCOverallSpinner.getValue());
        
        logger.info("Set Network Option: " + maxNOCPerHostSpinner.getValue() + " - " + maxNOCOverallSpinner.getValue());
        
        changed = false;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        maxNOCOverallSpinner = new javax.swing.JSpinner();
        maxNOCPerHostSpinner = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gui.WSFApplication.class).getContext().getResourceMap(NetworkPanel.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        maxNOCOverallSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        maxNOCOverallSpinner.setName("maxNOCOverallSpinner"); // NOI18N
        maxNOCOverallSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                maxNOCOverallSpinnerStateChanged(evt);
            }
        });

        maxNOCPerHostSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        maxNOCPerHostSpinner.setName("maxNOCPerHostSpinner"); // NOI18N
        maxNOCPerHostSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                maxNOCPerHostSpinnerStateChanged(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(maxNOCOverallSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(maxNOCPerHostSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(maxNOCPerHostSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxNOCOverallSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void maxNOCPerHostSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_maxNOCPerHostSpinnerStateChanged
        
        this.changed = true;
        
        int maxNOCPerHost = (Integer)maxNOCPerHostSpinner.getValue();
        int maxNOCOverall = (Integer)maxNOCOverallSpinner.getValue();
        
        if( maxNOCPerHost > maxNOCOverall ){
            maxNOCOverallSpinner.setValue(maxNOCPerHost);
        }
    }//GEN-LAST:event_maxNOCPerHostSpinnerStateChanged

    private void maxNOCOverallSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_maxNOCOverallSpinnerStateChanged
        
        this.changed = true;
        
        int maxNOCPerHost = (Integer)maxNOCPerHostSpinner.getValue();
        int maxNOCOverall = (Integer)maxNOCOverallSpinner.getValue();
        
        if( maxNOCPerHost > maxNOCOverall ){
            maxNOCPerHostSpinner.setValue(maxNOCOverall);
        }
    }//GEN-LAST:event_maxNOCOverallSpinnerStateChanged
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSpinner maxNOCOverallSpinner;
    private javax.swing.JSpinner maxNOCPerHostSpinner;
    // End of variables declaration//GEN-END:variables
    
}
